functionDefExpr ::= f[unctio]n symbol [ argSeq ] body
| f[unctio]n symbol ( [ argList ] ) body
methodDefExpr ::= method [ getterOrSetter ] symbol argSeq body
freeMethDefExpr ::= [ class ] method [ getterOrSetter ] symbol argSeq body
body ::= -> expr
The reserved words get
and set
are semantically equivalent to naming a method with "getter" and "setter" as suffixes. A method that is defined as a getter or setter acts as a virtual instance variable. For more information on getter and setter methods, see page 141.
getterOrSetter ::= get | set
A ScriptX function or method can accept three different kinds of arguments. Positional arguments are conventional, unkeyed arguments, and they must be supplied in the correct order when the function or method is called. Rest arguments, which are also unkeyed, provide a mechanism for passing in a variable number of arguments. Key arguments are passed with a keyword identifier, as a keyword-value pair, so they can be supplied in any order.
argSeq ::= [ positionalArgSeq ] [ restArg ] [ keywordArgSeq ]
argList ::= [ positionalArgList ] [ restArg ] [ keywordArgList ]
A function's argument definitions (parameters) are matched with arguments in the call expression's argument sequence. Call expressions are discussed on page 240. Positional arguments are matched first. A function or method accepts a set of positional arguments, separated by commas or spaces.
positionalArgSeq ::= argument [ argument ]*
positionalArgList ::= argument [ , positionalArgList ]*
An argument definition is a symbol. It resembles a local variable definition, naming a variable that is defined within its own body. Like a local variable declaration, an argument definition overrides a name that is defined in a surrounding scope. Restrictions are permitted as part of the argument syntax, but they are not implemented in the current version of ScriptX.
argument ::= symbol [ restriction ]
The rest argument clause supplies a symbol, which the call expression uses to pass a variable-length sequence of unkeyed arguments. When the function or method is called, this symbol is bound to an array, and all the arguments from the call expression's argument list are placed in sequence in that array. Rest arguments are useful when a function wants to accept a variable-length sequence of arguments.
restArgs ::= #rest symbol
The keyword argument sequence begins with the reserved word #key
, which serves as a lead-in for the sequence or list that follows. Keyword-argument pairs can be separated by blanks if parentheses are not used, or by commas if parentheses are used.
keywordArgSeq ::= #key keyArgPair [ keyArgPair ]*
keywordArgList ::= #key keyArgPair [ , keyArgPair ]*
Each keyword-argument definition can have three parts. Only the first part, the name of the keyword itself, is actually required. This argument defintion is the key that a calling function uses to specify which argument it is supplying. A function or method definition can supply an optional symbol, which will be the name of the argument inside the function body. Otherwise, the argument's name inside the function is the name of the keyword itself.
keyArgPair ::= argument : [ symbol ] [ ( simpleExpr ) ]
A function or method definition can also supply an optional expression, enclosed in parentheses, which is used to set a default value if a keyword argument is not supplied by the caller. It is up to the function or method itself to test for required keyword arguments, and report an exception if any are missing, or are supplied incorrectly.
An anonymous function, also called an in-line function definition, is a function definition that is not assigned to a lexical name. Unlike other functions and methods, it is a factor. Its argument definition list is in the same form as the argument definition list for a regular function definition. The body of an anonymous function must be a compound expression sequence. This sequence can be as simple as one inner-level expression.
anonFuncDefExpr ::= ( anonArgs -> compoundExprSeq )
anonArgs ::= [ paramSequence ] [ restArg ] [ keywordArgSeq ]
| empty
This document is part of the ScriptX Language Guide, one of the volumes of the ScriptX Technical Reference Series. ScriptX is developed by the ScriptX Engineering Team at Apple Computer, successor to the Kaleida Engineering Team at Kaleida Labs, Inc.